package com.example.ranl_offershow.Controller;

import com.example.ranl_offershow.Entity.Data.OfferData;
import com.example.ranl_offershow.Service.OfferDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api")
public class WebController {
    @Autowired
    private OfferDataService offerDataService;

    //展示所有帖子
    //实现增加帖子后的页面跳转
    @GetMapping("/offerdata")
    public String getAllOfferData(Model model) {
        List<OfferData> offerData = offerDataService.getAllOfferData();
        model.addAttribute("offerData",offerData);
        return "dataList";
    }

    @GetMapping("/offerdata/{id}")
    public ResponseEntity<OfferData> getOfferDataById(@PathVariable int id) {
        OfferData offerData = offerDataService.getOfferDataById(id);
        return ResponseEntity.ok(offerData);
    }

    //增加帖子数据
    @RequestMapping("/insertPage")
    public String showInsertPage() {
        return "insertPage"; // 返回到添加数据的页面
    }
    @RequestMapping("/insertofferdata")
    public String createOfferData(OfferData offerData) {
        offerDataService.createOfferData(offerData);
        return "redirect:/api/offerdata";
    }

    //更改帖子信息
    //更改完成后直接跳转回帖子列表
    @GetMapping("/updateofferdata/{id}")
    public String updateOfferData(Model model, @PathVariable int id) {
        OfferData offerData = offerDataService.getOfferDataById(id);
        model.addAttribute("offerData",offerData);
        return "updatePage";
    }
    @PostMapping("/update")
    public String updateData(OfferData offerData){
        offerDataService.updateOfferData(offerData);
        return "redirect:/api/offerdata";
    }

    //删除帖子数据
    //删除完成后直接跳转回帖子列表
    @GetMapping("/deleteofferdata/{id}")
    public String deleteOfferData(@PathVariable int id){
        offerDataService.deleteOfferData(id);
        return "redirect:/api/offerdata";
    }

    //根据公司名字进行部分检索
    @RequestMapping("/queryPage")
    public String showQueryPage() {
        return "queryPage"; // 返回到检索数据的页面
    }
    @RequestMapping("/queryofferdata")
    public String getOfferDataByCompany(String company, Model model) {
        List<OfferData> offerData = offerDataService.getOfferDataByCompany(company);
        model.addAttribute("offerData",offerData);
        return "queryDataList";
    }


    //根据公司名字进行批量删除
    @RequestMapping("/deletePage")
    public String showDeletePage() {
        return "deletePage"; // 返回到检索数据的页面
    }

    @PostMapping("/deleteBatchOfferData")
    public String deleteBatchOfferData(String company){
        offerDataService.deleteBatchOfferData(company);
        return "redirect:/api/offerdata";
    }
}
